package reduce.fz.com.reduce.activity;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.fastjson.TypeReference;
import com.ta.utdid2.android.utils.StringUtils;

import java.util.HashMap;

import javax.security.auth.Subject;

import reduce.fz.com.reduce.R;
import reduce.fz.com.reduce.abstracts.AbResponseListener;
import reduce.fz.com.reduce.bean.ServiceException;
import reduce.fz.com.reduce.bean.SubjectReply;
import reduce.fz.com.reduce.bean.TeamSubject;
import reduce.fz.com.reduce.bean.User;
import reduce.fz.com.reduce.bean.result.Result;
import reduce.fz.com.reduce.custom.LoadingDialog;
import reduce.fz.com.reduce.fragment.SubjectDetailFragment;
import reduce.fz.com.reduce.service.TeamService;
import reduce.fz.com.reduce.service.preference.UserPreference;
import reduce.fz.com.reduce.util.InputMethodUtil;
import reduce.fz.com.reduce.util.ServiceBuilder;

public class SubjectDetailActivity extends BaseToolBarActivity {
    private EditText etReplyContent;
    private TextView tvReplyBtn;
    private Long teamid;
    public TeamSubject teamSubject;
    private TeamService teamService= ServiceBuilder.builderTeamService(null);
    private SubjectDetailFragment fragment;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_subject_detail);
        setToolbarTitle("话题详情");
        etReplyContent= (EditText) findViewById(R.id.et_reply_content);
        tvReplyBtn=(TextView)findViewById(R.id.tv_reply_btn);
        teamid=getIntent().getLongExtra("teamid",0);
        teamSubject= (TeamSubject) getIntent().getSerializableExtra("subject");
        initEditListener();
        initReplyBtnListener();
        attachDetailFragment();
    }


    public void attachDetailFragment()
    {
        fragment=SubjectDetailFragment.create(teamid,teamSubject);
        getSupportFragmentManager().beginTransaction().replace(R.id.subject_reply_container,fragment).commit();
    }

    @Override
    public void initWidget() {

    }

    @Override
    public void initListener() {

    }



    public void initEditListener() {
        etReplyContent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //获取焦点
                InputMethodUtil.reuestFocusAndShowInput(etReplyContent);
            }
        });
    }

    public void initReplyBtnListener() {

        tvReplyBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //清除焦点并隐藏输入法
                reply(etReplyContent.getText().toString());
                InputMethodUtil.clearFocus(etReplyContent);
                etReplyContent.setText("");


            }
        });

    }

    public void reply(String desc)
    {
        if(StringUtils.isEmpty(desc)){
            toastMsg("内容不能为空!");
            InputMethodUtil.reuestFocusAndShowInput(etReplyContent);
        }
        final LoadingDialog dialog=new LoadingDialog(this);
        dialog.setMsg("提交中!").show();
        final SubjectReply reply=new SubjectReply();
        User user= UserPreference.getUser();
        reply.user=user;
        reply.teamid=teamid;
        reply.description=desc;
        reply.subjectid=teamSubject.subjectid;
        reply.pubtime=System.currentTimeMillis();
        teamService.pubSubjectReply(reply, new AbResponseListener<HashMap>(new TypeReference<Result<HashMap>>() {
        }) {
            @Override
            public void error(ServiceException e) {
                dialog.dismiss();

            }

            @Override
            public void responseInfo(Result<HashMap> data) {
                dialog.dismiss();
                fragment.addReply(reply);
            }
        });
    }



}
